package com.kcwl.carrier.application.service;

import com.kcwl.carrier.domain.entity.VehicleBlacklistCondition;
import com.kcwl.carrier.domain.entity.VehicleBlacklistInfo;
import com.kcwl.carrier.domain.entity.VehicleInfo;
import com.kcwl.carrier.domain.service.VehicleBlacklistDomainService;
import com.kcwl.carrier.domain.service.VehicleInfoDomainService;
import com.kcwl.carrier.interfaces.command.VehicleBlacklistInfoCommand;
import com.kcwl.carrier.interfaces.dto.VehicleBlacklistInfoDTO;
import com.kcwl.carrier.interfaces.query.VehicleBlackListPageByUserQuery;
import com.kcwl.carrier.interfaces.query.VehicleBlacklistQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.infrastructure.utils.KcPageConverter;
import com.kcwl.ddd.interfaces.dto.PageInfoDTO;
import com.kcwl.framework.utils.JsonUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class VehicleBlacklistApplicationService {

    @Autowired
    VehicleBlacklistDomainService vehicleBlacklistDomainService;
    @Autowired
    VehicleInfoDomainService vehicleInfoDomainService;

    public VehicleBlacklistInfoDTO addBlacklist(VehicleBlacklistInfoCommand command) {
        command.setCreateTime(new Date());

        // 根据车牌查询黑名单列表
        VehicleBlacklistCondition condition = new VehicleBlacklistCondition();
        condition.setPlateNumber(command.getPlateNumber());
        condition.setIsDel(YesNoEnum.NAY.getValue());
        List<VehicleBlacklistInfo> blacklistInfoList = vehicleBlacklistDomainService.queryVehicleBlacklistList(condition);

        // 存在该车辆黑名单更新
        if (blacklistInfoList.size() > 0) {
            command.setId(blacklistInfoList.get(0).getId());
            vehicleBlacklistDomainService.updateBlacklist(KcBeanConverter.toBean(command, VehicleBlacklistInfo.class));
        } else {
            // 不存在该车辆黑名单新增
            Long id = vehicleBlacklistDomainService.addBlacklist(KcBeanConverter.toBean(command, VehicleBlacklistInfo.class));
            command.setId(id);
        }

        return KcBeanConverter.toBean(command, VehicleBlacklistInfoDTO.class);
    }

    public PageInfoDTO<VehicleBlacklistInfoDTO> queryVehicleBlacklistPage(VehicleBlacklistQuery query) {
        return KcPageConverter.toPageInfoDTO(vehicleBlacklistDomainService.queryVehicleBlacklistPage(query), VehicleBlacklistInfoDTO.class);
    }

    public Boolean updateBlacklist(VehicleBlacklistInfoCommand command) {
        command.setUpdateTime(new Date());
        return vehicleBlacklistDomainService.updateBlacklist(KcBeanConverter.toBean(command, VehicleBlacklistInfo.class));
    }

    public List<VehicleBlacklistInfoDTO> queryVehicleBlacklist(VehicleBlacklistQuery query) {
        return KcBeanConverter.toList(vehicleBlacklistDomainService.queryVehicleBlacklistList(KcBeanConverter.toBean(query, VehicleBlacklistCondition.class)), VehicleBlacklistInfoDTO.class);
    }

    /**
     * 用户下的车辆黑名单
     */
    public PageInfoDTO<VehicleBlacklistInfoDTO> queryPageListByUser(VehicleBlackListPageByUserQuery query) {

        //获取名下车辆id
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setCarrierId(query.getUserId());
        vehicleInfo.setIsDel(0);
        List<VehicleInfo> vehicleInfoList = vehicleInfoDomainService.queryList(vehicleInfo);
        log.info("车主 userId= {}, 名下车辆信息 = {}",query.getUserId(), JsonUtil.toJson(vehicleInfoList));
        if (vehicleInfoList.isEmpty()) {
            return new PageInfoDTO<>(0, query.getCurPagerNo(), query.getPageSize(), new ArrayList<>());
        }
        Map<Long, VehicleInfo> vehicleInfoMap = vehicleInfoList.stream().collect(Collectors.toMap(VehicleInfo::getId, e -> e));
        log.info("车主 userId= {}, 转map = {}",query.getUserId(), JsonUtil.toJson(vehicleInfoMap));
        List<Long> vehicleIds = vehicleInfoList.stream().map(VehicleInfo::getId).collect(Collectors.toList());
        VehicleBlacklistQuery condition = KcBeanConverter.toBean(query, VehicleBlacklistQuery.class);
        condition.setVehicleIds(vehicleIds);
        log.info("车主 userId= {}, 查询黑名单列表参数condition  = {}",query.getUserId(),JsonUtil.toJson(condition) );
        KcPage<VehicleBlacklistInfo> blacklistInfoKcPage = vehicleBlacklistDomainService.queryVehicleBlacklistPage(condition);
        log.info("车主 userId= {}, 查询黑名单列表参数condition  = {},返回值 =【{}】，",query.getUserId(),JsonUtil.toJson(condition) ,JsonUtil.toJson(blacklistInfoKcPage));
        PageInfoDTO<VehicleBlacklistInfoDTO> dtoPageInfoDTO = KcPageConverter.toPageInfoDTO(blacklistInfoKcPage, VehicleBlacklistInfoDTO.class);
        log.info("车主 userId= {}, 查询黑名单列表参数condition  = {},返回值 =【{}】，转DTO = 【{}】",
                query.getUserId(),JsonUtil.toJson(condition) ,JsonUtil.toJson(blacklistInfoKcPage),JsonUtil.toJson(dtoPageInfoDTO));

        List<VehicleBlacklistInfoDTO> vehicleBlacklistInfoDTOList = dtoPageInfoDTO.getList();
        //组装黑名单详情 车辆载重（吨）loads  车牌颜色 plate_color 能源类行 vehicle_category  车辆类型 vehicle_type
        if (!vehicleBlacklistInfoDTOList.isEmpty()) {
            for (VehicleBlacklistInfoDTO vehicleBlacklistInfoDTO : vehicleBlacklistInfoDTOList) {
                VehicleInfo vInfo = vehicleInfoMap.get(vehicleBlacklistInfoDTO.getVehicleId());
                vehicleBlacklistInfoDTO.setLoads(vInfo.getLoads());
                vehicleBlacklistInfoDTO.setPlateColor(vInfo.getPlateColor());
                vehicleBlacklistInfoDTO.setVehicleCategory(vInfo.getVehicleCategory());
                vehicleBlacklistInfoDTO.setVehicleType(vInfo.getVehicleType());
            }
        }
        log.info("车主 userId= {}, 查询黑名单列表参数condition  = {},返回值 =【{}】,最终结果 ={}",
                query.getUserId(),JsonUtil.toJson(condition) ,JsonUtil.toJson(blacklistInfoKcPage),JsonUtil.toJson(dtoPageInfoDTO));
        return dtoPageInfoDTO;
    }
}
